<script setup lang="ts">
import { useSystemStore } from '@/store/main/system/system'
import type { ElForm } from 'element-plus'
import { reactive, ref } from 'vue'
// 定义属性
const centerDialogVisible = ref(false)
// 记录是否为新建用户
const isNewRef = ref(true)
// props
interface IModalConfig {
  modalConfig: {
    pageName: string
    header: any
    formItems: any[]
  }
}

const props = defineProps<IModalConfig>()
// 表单数据
const initialData: any = {}
for (const item of props.modalConfig.formItems) {
  initialData[item.prop] = item.initialValue ?? ''
}
const pageFormValue = reactive<any>(initialData)
const dialogFormRef = ref<InstanceType<typeof ElForm>>()
// 获取角色和部门
// const useRoleAndDepart = useEntriesDataStore()
const userStore = useSystemStore()
//遍历的是部门的数据 现在不能写死
// const { allDepartments } = storeToRefs(useRoleAndDepart)
// 记录编辑数据
const editData = ref()

function changeDialogVisible(isNew: boolean = true, itemData?: any) {
  centerDialogVisible.value = true
  isNewRef.value = isNew
  if (!isNew && itemData) {
    for (const key in pageFormValue) {
      pageFormValue[key] = itemData[key]
    }
    editData.value = itemData
  } else {
    for (const key in pageFormValue) {
      pageFormValue[key] = ''
    }
    editData.value = null
  }
}
// 确定按钮
function handleConfirmClick() {
  // 关闭dialog
  centerDialogVisible.value = false
  // 将form数据传入 创建新用户 发送网络请求
  if (!isNewRef.value && editData.value) {
    userStore.editPageItemByFormAction(
      props.modalConfig.pageName,
      editData.value.id,
      pageFormValue
    )
  } else {
    userStore.addNewPageItemAction(props.modalConfig.pageName, pageFormValue)
  }
}
defineExpose({ changeDialogVisible })
</script>
<template>
  <div class="user-model">
    <el-dialog
      v-model="centerDialogVisible"
      :title="
        isNewRef
          ? modalConfig.header.isNewTitle
          : modalConfig.header.isEditTitle
      "
      width="30%"
      center
    >
      <div class="formUser">
        <el-form
          ref="dialogFormRef"
          label-width="100px"
          :model="pageFormValue"
          style="max-width: 460px"
          label-position="right"
          size="large"
        >
          <template v-for="item in modalConfig.formItems" :key="item.prop">
            <el-form-item :label="item.label" :prop="item.prop">
              <template v-if="item.type === 'normal'">
                <el-input
                  v-model="pageFormValue[item.prop]"
                  :placeholder="item.placeholder"
                />
              </template>
              <template v-if="item.type === 'select'">
                <el-select
                  v-model="pageFormValue[item.prop]"
                  :placeholder="item.placeholder"
                  size="large"
                  style="width: 100%"
                >
                  <template v-for="op in item.options" :key="op.value">
                    <el-option :label="op.label" :value="op.value" />
                  </template>
                </el-select>
              </template>
            </el-form-item>
          </template>
        </el-form>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="centerDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleConfirmClick">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<style scoped lang="less">
.user-model {
}
</style>
